{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9612de77",
   "metadata": {},
   "source": [
    "# 本周知识点\n",
    "\n",
    " 1.有序的数据结构-字符串   \n",
    "    \n",
    " 1.字符串的基本概念（回顾）  \n",
    "     \n",
    "1.Single quotes: 'allows embedded \"double\" quotes'    \n",
    "2.Double quotes: \"allows embedded 'single' quotes\"   \n",
    "3.Triple quoted: '''Three single quotes''', \"\"\"Three double quotes\"\"\"     \n",
    " 2.字符串的性质和方法   \n",
    "    \n",
    " 3.有序的数据结构-列表   \n",
    "     \n",
    " ### 1. 字符串的性质和方法\n",
    "\n",
    "·字符串的性质   \n",
    "1.字符串的有序性\n",
    "2.字符串是有长度的\n",
    "\n",
    "     \n",
    "     1.使用len()检查字符串的长度\n",
    "     \n",
    "     \n",
    "·字符串的基础方法（运算法则）    \n",
    "\n",
    "\n",
    "1.字符串的取值:str[index]     \n",
    "2.字符串的切片：str[start,stop],stop取不到       \n",
    "3.字符串的拼接：+      \n",
    "4.in 检查成员是否存在 \n",
    "\n",
    "\n",
    "·字符串的一般方法（function/Methods 函数/方法）     \n",
    "\n",
    "1.isdigit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8406fdfc",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = \"广州南方学院团委\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a2378945",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "026aa56e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'州'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f934626e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'委'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "587afd26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'团'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "ac41288a",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "string index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[0;32mIn [35]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mtext\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m100\u001b[39;49m\u001b[43m]\u001b[49m\n",
      "\u001b[0;31mIndexError\u001b[0m: string index out of range"
     ]
    }
   ],
   "source": [
    " text[100]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e3a2d1d4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "47f9dbd4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州南'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[0] + text[1] + text[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b7ad7a43",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州南方学院'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[0:6] # 等价于 text[:6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5442c965",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州南方学院'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[:6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7925a0be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "''"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[-1:-3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5c9200b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'院团'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[-3:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d2621b8a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'团委'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[-2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "7ffb2e80",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州南方学院团委广州南方学院团委广州南方学院团委'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 * text"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60ad8070",
   "metadata": {},
   "source": [
    "·in 检查成员对象        \n",
    "\n",
    "·count()检查字符串出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "fdd26e1c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"广州\" in text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "21ab91cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"北京\" in text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "5e3c70e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 * text.count(\"广州\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd8b5d3f",
   "metadata": {},
   "source": [
    "·isdigit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "77219bc8",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_1 = 19\n",
    "age_2 = \"19\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "a90f959c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19 <class 'int'>\n",
      "19 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(age_1,type(age_1))\n",
    "print(age_2,type(age_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "6495751d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "38\n"
     ]
    }
   ],
   "source": [
    "# 如果字符串是数值，可以用int转换成真正的数值\n",
    "if age_2.isdigit():\n",
    "    print(age_1 + int(age_2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6fd7e0a",
   "metadata": {},
   "source": [
    " ### 2.有序的数据结构-列表（List）\n",
    "\n",
    "·1.列表的概念和基本性质       \n",
    "\n",
    "1.Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.         \n",
    "\n",
    "2.列表一般方法(如何学习)        \n",
    "\n",
    "1.使用.Tab键查看列表的方法       \n",
    " #### 2.1 列表的性质 \n",
    "\n",
    "·和字符串的运算法则完全一致                 \n",
    "·注意： 字符串是对字符进行操作，列表是对items元素进行操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "40792516",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列表的形式\n",
    "squares = [1, 4, 9, 16, 25] # 列表里面放数值\n",
    "squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "c2adc16a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "25"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squares[4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "c2e3d454",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "4 in squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "6cee6fd0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squares.count(9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "dd91cc88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', 'Jan']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names = [\"Jack\",\"Alex\",\"Marry\",\"Jan\"] # 列表里面放字符串\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "c8b6f20b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25, 'Jack', 'Alex', 'Marry', 'Jan']"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " squares + names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "14bf5384",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25, 1, 4, 9, 16, 25]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2*squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "99412847",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Alex', 'Marry']"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "fa756071",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Jan']"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names[-2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "df791c62",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Marry']"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取出\"Jack\",\"Marry\" 用切片的方法\n",
    "names[:3:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5321503e",
   "metadata": {},
   "source": [
    "### ·2.列表一般方法(如何学习)\n",
    "1.增加      \n",
    "2.删除       \n",
    "3.改     \n",
    "4.查 ：in      \n",
    "\n",
    "·列表的学习方法(步骤)   \n",
    "\n",
    "1.使用.Tab键查看列表的方法       \n",
    "2.使用help(查看方法的使用规则)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "19bc2d72",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', 'Jan']"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names = [\"Jack\",\"Alex\",\"Marry\",\"Jan\"]\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "9b3dce5a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.append)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "a798a749",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.append(\"Lihua\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d99b23d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8c75f8d",
   "metadata": {},
   "source": [
    " # 课后作业\n",
    " ## 列表总结和示例"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc12bedc",
   "metadata": {},
   "source": [
    "|列表方法|说明|使用方法| \n",
    "|---------|-----|-----|\n",
    "|append()|在列表末尾添加新的对象|list.append(obj)|\n",
    "|count()|统计某个元素在列表中出现的次数|list.count(obj)|\n",
    "|extend()|在列表末尾一次性追加另一个序列中的多个值|list.extend(seq)|\n",
    "|insert()|将对象插入列表|list.insert(index, obj)|\n",
    "|index()|从列表中找出某个值第一个匹配项的索引位置|list.index(obj)|\n",
    "|pop()|移除列表中的一个元素（默认最后一个元素），并且返回该元素的值|list.pop()=list[-1])|\n",
    "|remove()|移除列表中某个值的第一个匹配项|list.remove(obj)|\n",
    "|reverse()|反向列表中元素|list.reverse()|\n",
    "|sort()|对原列表进行排序|list.sort( key=None, reverse=False)|\n",
    "|clear()|清空列表|list.clear()|\n",
    "|copy()|复制列表|list.copy()|\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf15fa71",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
